/***
 * 扩展数组的删除一个元素的方法
 */
Array.prototype.removeByValue = function(val) {
	for(var i=0; i<this.length; i++) {
		if(this[i] == val) {
			this.splice(i, 1);
	      	//break;
	 	}
	}
};
/***
 * 扩展数据的新增元素的方法
 */
Array.prototype.addByValue=function(val){
	if(this.indexOf(val)<0){
		this.push(val);
	}
}


/***
 * bootbox的默认参数
 */

bootbox.setDefaults({
	locale:'zh_CN',
	size: 'small',
	title:'提示'
});


(function($){
	$.fn.serializeObject = function(){
		var o = {};
		var a = this.serializeArray();
		$.each(a, function() {
		    if (o[this.name] !== undefined) {
		        if (!o[this.name].push) {
		            o[this.name] = [o[this.name]];
		        }
		        o[this.name].push(this.value || '');
		    } else {
		        o[this.name] = this.value || '';
		    }
		});
		return o;
	};
	
	$.fn.loadHtml=function(url,params,callback){
		var self =$(this);
		self.load(url,params,function(){
			$.parser.parse(self);
			if(callback){
				callback(self.children());
			}
		})
	}
		
	$.fn.form=function(option,params){
		if(typeof option=='string'){
			method=$.fn.form.methods[option];
			return method(this,params);
		}
		return;
	};
	
	$.fn.form.methods={
		'clear':function(jq){
			jq.find('input.combobox').each(function(){
				$(this).combobox('setValue','');
			})
			jq.find('input').val('');
		},
		'reset':function(jq){
			jq[0].reset();
			jq.find('input.combobox').each(function(){
				$(this).combobox('setValue',$(this).val());
			});
			jq.find('input.combotree').each(function(){
				$(this).combotree('setValue',$(this).val());
			})
		},
		
		
		
		'getParams':function(jq){
			return jq.serializeObject();
		}
	}
	
	$.extend({
		'messagebox':function(text,time){
			var div=$('<div id="pop" class="well " style=" width: 200px;position:fixed;right: 10px;bottom:0;margin-bottom: 70px;" hidden>');
			var span=$('<span class="glyphicon glyphicon-ok-sign"></span><span class="popContext"> 保存成功</span>')
			div.append(span);
			$('body').append(div);
			if(text==undefined)
			        text = "修改成功";
			if(time==undefined)
					time = 2000;
			 
			    div.find('span.popContext').html(" "+text);
			 
			    div.show(200,"swing",function(){
					setTimeout(function(){
						  div.hide(200,"swing",function(){
							  div.remove();
						  });
							
					},time);
				})
		},
		
	})
	
	
	$.extend({
		'createBtn':function(options){
			var btn=$('<button type="button" class="btn btn-default"/>');
			if(options.iconCls){
				var span=$('<span aria-hidden="true"/>');
				span.attr('class',options.iconCls);
				btn.append(span);
				btn.append('&nbsp;');
			}
			if(options.text){
				btn.append(options.text);
			}
			if(options.btnCls){
				btn.attr('class',options.btnCls);
			}
			if(options.type){
				btn.attr('type',options.type);
			}
			if(options.handler){
				btn.click(options.handler);
			}
			if(options.isCloseBtn){
				btn.attr('data-dismiss','modal');
			}
			return btn;
		},
		'createDialog':function(options){
			var dialog=$('<div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"/>');
			$('body').append(dialog);
			var modal_dialog= $('<div class="modal-dialog" role="document"/>');
			modal_dialog.appendTo(dialog);
			var modal_content = $('<div class="modal-content"/>');
			modal_content.appendTo(modal_dialog);
			var modal_header=$('<div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button><h4 class="modal-title" id="myModalLabel">对话框</h4></div>');
			if(options.title){
				modal_header.find('h4').text(options.title);
			}
			var modal_body=$('<div class="modal-body"/>');
			var modal_footer=$('<div class="modal-footer"/>');
			modal_content.append(modal_header);
			modal_content.append(modal_body);
			modal_content.append(modal_footer);
			if(options.url){
				modal_body.loadHtml(options.url,options.postData,function(){
					var form=modal_body.find('form');
					if(form.length){
						if(options.onsubmit){
							form.bootstrapValidator().on('success.form.bv',options.onsubmit);
						}else{
							form.bootstrapValidator();
						}
					}
				});
			}
			if(options.buttons){
				for(var i=0;i<options.buttons.length;i++){
					modal_footer.append(options.buttons[i]);
				}
			}
			dialog.modal({
				show:false
			});
			return dialog;
		}	
	});
	
	//向$.fn中扩展datagrid对象
	$.extend($.fn,{
		'datagrid':function(options,params){
			if(typeof options == 'string'){
				var method = $.fn.datagrid.methods[options];
				return method(this,params);
			}
			options=options||{};
			return this.each(function(){
				var self=$(this);
				var _opts=self.data('datagrid');
				if(_opts){
					options=$.extend({},_opts,options);
					$.fn.datagrid.methods.destroy(self);
				}
				var idStr=Math.floor(Math.random()*100000000);
				var searchFormId='searchForm'+idStr;
				var toolbarId='toolbar'+idStr;
				var data_optionsStr='{'+this.dataset['options']+'}';
				var data_options=eval('('+data_optionsStr+')');
				var opts=$.extend({},$.fn.datagrid.defaults,data_options,options);
				if(opts.path){
					opts.url=opts.path+'/'+opts.url;
					opts.editUrl=opts.path+'/'+opts.editUrl;
					opts.deleteUrl=opts.path+'/'+opts.deleteUrl;
					opts.searchUrl=opts.path+'/'+opts.searchUrl;
					opts.saveUrl=opts.path+'/'+opts.saveUrl;
					opts.path='';
				}
				opts.toolbar='#'+toolbarId;
				opts.searchForm='#'+searchFormId;
				opts.queryParams=function(param){
					var obj=$(opts.searchForm).find('form').serializeObject();
					obj.limit=param.limit;
					obj.offset=param.offset;
					obj.search=param.search;
					return obj;
				}
				/***
				 * 如果有treegrid，加入onLoadSuccess方法去实现treegrid
				 */
				if(opts.treegrid){
					opts.onResetView=function() {
						self.treegrid({
							//initialState: 'collapsed',
							treeColumn: 1,
							expanderExpandedClass: 'glyphicon glyphicon-minus',
							expanderCollapsedClass: 'glyphicon glyphicon-plus',
							onChange: function() {
								self.bootstrapTable('resetWidth');
							}
						});
					}
					opts.search=false;
					opts.showColumns=false;
					opts.showToggle=false;
					//opts.showSearchButton=true;
				}

				
				self.data('datagrid',opts);
				var searchForm=$.createDialog({
					title:'查询',
					url:opts.searchUrl,
					onsubmit:function(e){e.preventDefault()},
					buttons:[$.createBtn({
						iconCls:'glyphicon glyphicon-search',
						text:'查询',
						btnCls:'btn btn-primary',
						isCloseBtn:true,
						handler:function(){
							self.bootstrapTable('selectPage',1);
						}
					}),$.createBtn({
						iconCls:'glyphicon glyphicon-remove',
						text:'清除',
						isCloseBtn:true,
						handler:function(){
							var form=searchForm.find('form');
							form.form('clear');
							self.bootstrapTable('selectPage',1);
						}
					})]
				});
				searchForm.attr('id',searchFormId);
				self.after(searchForm);
				var toolbar=$('<div class="btn-group"/>');
				toolbar.attr('id',toolbarId);
				searchForm.after(toolbar);
				for(var i=0;i<opts.buttons.length;i++){
					var btnName=opts.buttons[i];
					if(typeof btnName=='string'){
						toolbar.append($.fn.datagrid.toolbar.buttons[btnName]());
					}else if(typeof btnName=='function'){
						toolbar.append(btnName());
					}else{
						toolbar.append(btnName);
					}
				}
				/**
				 * 为数据刷新后保留选中行
				 */
				opts.onPostBody=function(data){
					var options2=self.data('datagrid');
					var idField=options2.idField;
					var ids=options2.selectIds;
					self.bootstrapTable('checkBy',{field:idField,values:ids});
					ids.length=0;
				}
				//用bootstrapTable初始化表格
				return self.bootstrapTable(opts);
			})
		}
	});
	
	//扩展$.fn.datagrid的toolbar对象
	$.fn.datagrid.toolbar={
		'grid':function(btn){
			return $(btn).closest('div.bootstrap-table').find('div.fixed-table-container>div.fixed-table-body>table');
		},
		'buttons':{
			'add':function(){  
				return $.createBtn({
				iconCls:'glyphicon glyphicon-plus',
				text:'新增',				
				handler:function(){
					var self=$(this);
					var grid=$.fn.datagrid.toolbar.grid(self);
					var editUrl=grid.datagrid('options').editUrl;
					var saveUrl=grid.datagrid('options').saveUrl;
					var dialog=$.createDialog({
						title:'新增',
						url:editUrl,
						onsubmit:function(e){e.preventDefault()},
						buttons:[$.createBtn({
							iconCls:'glyphicon glyphicon-floppy-disk',
							text:'保存',
							btnCls:'btn btn-primary',
							handler:function(){
								var form=dialog.find('form');
								var validator=form.data('bootstrapValidator');
								validator.validate();
								if(validator.isValid()){
									var fd=new FormData(form[0]);
									$.ajax({
										url : saveUrl,
										type : "POST",
										data : fd,
										processData : false, // 不处理数据  
										contentType : false, // 不设置内容类型
										success : function(result) {
											if (result.success) {
												dialog.modal("hide");
												$.messagebox(result.msg,2000);
												grid.bootstrapTable('refresh');
											} else {
												bootbox.alert(result.msg);
											}
										},
										error : function(result) {
											bootbox.alert("保存请求发送失败，请检查网络是否连通");
										}
									});
								}
							}
						}),$.createBtn({
							iconCls:'glyphicon glyphicon-remove',
							text:'关闭',
							isCloseBtn:true
						})]
					}).on('hidden.bs.modal',function(){
						dialog.remove();
					})
					dialog.modal('show');
				}
			})
			},
			'edit':function(){
				return $.createBtn({
				iconCls:'glyphicon glyphicon-pencil',
				text:'修改',
				handler:function(){
					var self=$(this);
					var grid=$.fn.datagrid.toolbar.grid(self);
					var options=grid.datagrid('options');
					var row=grid.bootstrapTable('getSelections');
					var id=options.idField;
					var postData={};			
					if(row.length==1){
						postData[id]=row[0].id;
					}else{
						bootbox.alert('请选择一行再进行此操作');
						return;
					}
					
					var editUrl=grid.datagrid('options').editUrl;
					var saveUrl=grid.datagrid('options').saveUrl;
					var dialog=$.createDialog({
						title:'修改',
						url:editUrl,
						postData:postData,
						onsubmit:function(e){e.preventDefault()},
						buttons:[$.createBtn({
							iconCls:'glyphicon glyphicon-floppy-disk',
							text:'保存',
							btnCls:'btn btn-primary',
							handler:function(){
								var form=dialog.find('form');
								var validator=form.data('bootstrapValidator');
								validator.validate();
								if(validator.isValid()){
									var fd=new FormData(form[0]);
									$.ajax({
										url : saveUrl,
										type : "POST",
										data : fd,
										processData : false, // 不处理数据  
										contentType : false, // 不设置内容类型
										success : function(result) {
											if (result.success) {
												dialog.modal("hide");
												$.messagebox(result.msg,2000);
												grid.bootstrapTable('refresh');
											} else {
												bootbox.alert(result.msg);
											}
										},
										error : function(result) {
											bootbox.alert("保存请求发送失败，请检查网络是否连通");
										}
									});
								}
							}
						}),$.createBtn({
							iconCls:'glyphicon glyphicon-share-alt',
							text:'重置',
							handler:function(){
								var form=$(this).closest('.modal-content').find('.modal-body>form');
								form.form('reset');
							}
						}),$.createBtn({
							iconCls:'glyphicon glyphicon-remove',
							text:'关闭',
							isCloseBtn:true
						})]
					}).on('hidden.bs.modal',function(){
						dialog.remove();
					})
					dialog.modal('show');
					
				},
			})
			},
			
			'remove':function(){
				return $.createBtn({
				iconCls:'glyphicon glyphicon-remove',
				text:'删除',
				handler:function(){
					var self=$(this);
					var grid=$.fn.datagrid.toolbar.grid(self);
					var row=grid.bootstrapTable('getSelections');
					if(row.length){
						id=row[0].id;
					}else{
						bootbox.alert('请选择一行再进行此操作');
						return;
					}
					var deleteUrl=grid.datagrid('options').deleteUrl+"?id="+id;
					bootbox.confirm({
						message:'是否删除这条数据！',
						callback:function (value){
							if(value){
								 $.post(deleteUrl, {id:id}, function(result){
									 if(result.success){
										 $.messagebox(result.msg,2000);
										 grid.bootstrapTable('refresh');
										}else{
											bootbox.alert(result.msg);
										}
								 })
							}
						},
						buttons:{
							confirm:{
								confirm:'确认',
								className:'btn-primary'
							},
							cancel:{
								confirm:'取消',
								className:'btn-default'
							}
						}
					})
				}
			})
			},
			'search':function(){
				return $.createBtn({
				iconCls:'glyphicon glyphicon-search',
				text:'查询',
				handler:function(){
					var self=$(this);
					var grid=$.fn.datagrid.toolbar.grid(self);
					var options=grid.datagrid('options');
					var searchForm = $(options.searchForm);
					searchForm.modal('show');
				}
			})
			}
		}
	}

	
	$.fn.datagrid.defaults={
		path:'', 							//基础路径
		url:'page',                         //分页路径
		editUrl:'edit',	
		searchUrl:'search',
		deleteUrl:'delete',
		saveUrl:'save',
		idField: 'id',
		pagination: true,                   //是否显示分页（*）
		sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
		pageNumber: 1,                      //初始化加载第一页，默认第一页
		pageSize: 10,                       //每页的记录行数（*）
		pageList: [5,10,15],        //可供选择的每页的行数（*）
		method: 'get',
		search: true,                       //是否显示表格搜索，
		//striped:true,
		singleSelect:true,					//是否单选
		strictSearch: true,
		showColumns: true,                  //是否显示所有的列
		cache:false,
		showRefresh: true,                  //是否显示刷新按钮
		minimumCountColumns: 2,             //最少允许的列数
		clickToSelect: true,                //是否启用点击选中行
		showToggle: true,
		selectIds:[],						//选中的行的id号
		buttons: [$.fn.datagrid.toolbar.buttons.add,$.fn.datagrid.toolbar.buttons.edit,$.fn.datagrid.toolbar.buttons.remove,$.fn.datagrid.toolbar.buttons.search]
	}
	
	$.fn.datagrid.methods={
		'options':function(jq){
			return jq.data('datagrid');
		},	
		'destroy':function(jq){
			var opts=jq.data('datagrid');
			var searchForm=$(opts.searchForm);
			var toolbar=$(opts.toolbar);
			jq.bootstrapTable('destroy');
			jq.removeData('datagrid');
			searchForm.remove();
			toolbar.remove();
		},
		'refresh':function(jq){
			var opts=jq.data('datagrid');
			var idField=opts.idField;
			var ids=opts.selectIds;
			ids.length=0;
			var selections=jq.bootstrapTable('getSelections');
			for(var i=0;i<selections.length;i++){
				ids.push(selections[i][idField]);
				
			}
			jq.bootstrapTable('refresh');
		}
	}
	
	/***
	 * 扩展combotree,使用bootstrap-treeview插件
	 */
	
	$.extend($.fn,{
			'combotree' : function(options,params){
				var that=this;
				
				/* 先扩展几个create中要用到的方法 */
				this.onBodyDown=function(event){
					var opts=undefined;
					if($(event.target).hasClass('icon')){
						return;
					}
					try{
						opts=$.data($(event.target).parent().prev()[0],'combotree');
					}catch (e) {
						opts=undefined;
					}
					if (!(opts || event.target.id == 'comboDiv' || $(event.target).parents("#comboDiv").length>0)) {
						that.hideMenu(event);
					}
				}
				
				/* 隐藏下拉列表 */
				this.hideMenu=function(){
					$('#comboDiv').fadeOut('fast');
					$("#comboDiv").remove();
					$("body").unbind("click", that.onBodyDown);
				}
				
				/* 从节点数组里找出目标节点 */
				this.getNode=function(root,id){
					for(var i=0;i<root.length;i++){
						var node=root[i];
						if(node.id==id){
							return node;
						}
						var x=that.getNode(node.nodes?node.nodes:[],id);
						if(x){
							return x;
						}
					}
					return null;
				}
				
				/* 同步ajax调用,从后台获取树的节点数据 */
				this.getData = function(url,data){
					var result=$.ajax({
						type : 'post',
						url : url ,
						data: data,
						dataType : 'json' ,
						async : false
					}).responseText;
					return eval('('+result+')');
				}
				
				/* 创建UI的方法 */
				this.create=function(jq,options){
					jq.hide();
					var input_group=$('<div class="input-group"/>');
					jq.after(input_group);
					var textInp=$('<input readonly class="form-control" />');
					textInp.attr('placeholder',jq.attr('placeholder'));
					textInp.appendTo(input_group);
					var span=$('<span class="input-group-btn"></span>');
					var clear_btn=$('<button class="btn btn-default" type="button"><span class="glyphicon glyphicon-remove"></span></button>');
					clear_btn.click(function(){
						jq.combotree('clear');
						if(options.onSelected){
							options.onSelected();
						}
					})
					textInp.after(span);
					span.append(clear_btn);
					//生成opts
					var opts=$.extend({},options,{
						onNodeSelected:function(event,node){
							jq.val(node.id);
							textInp.val(node.text);
							that.hideMenu();
							if(options.onSelected){
								options.onSelected(node);
							}
						}
					});
					//在显示的框中写入默认值对应的text的内容
					var value=jq.val();
					if(value){
						var node=that.getNode(opts.data,value);
						if(node){
							textInp.val(node.text);
						}
					}
					
					textInp.click(function(e){
						if($('#comboDiv').length>0){
							that.hideMenu();
							e.preventDefault();
							return;
						}
						var div=$('<div id="comboDiv" class="panel panel-default" style="min-width:50%; max-height: 250px;overflow: auto;"><div class="panel-body"><div id="bootstrapCombotree"></div></div></div>');
						div.appendTo(jq.parent());
						$('#bootstrapCombotree').treeview(opts);
						var textInp_offset = textInp.parent().position();
						div.css({
							position: 'absolute',
							left : textInp_offset.left+'px',
							top: textInp_offset.top+textInp.outerHeight()+3+'px',
							zIndex: 1100
						}).slideDown('fast');
						$('body').bind('click',that.onBodyDown);
					});
					return jq;
				}
				
				if(typeof options =='string'){
					var method=$.fn.combotree.methods[options];
					return method(this,params);
				}
				
				options=options||{};
				return this.each(function(){
					var self=$(this);
					var state=self.data('combotree');
					if(state){
						options=$.extend({},state,options);
						$.fn.combotree.methods.destroy(self);
					}
					
					var data_options=this.dataset;
					var opts=$.extend({},$.fn.combotree.defaults,data_options,options);
					if(opts.url){
						opts.data=that.getData(opts.url,opts.postData);
						opts.url='';
					}
					self.data('combotree',opts);
					return that.create(self,opts);
				})
				
			}
		})
		
		$.fn.combotree.getNode=function(root,id){
			for(var i=0;i<root.length;i++){
				var node=root[i];
				if(node.id==id){
					return node;
				}
				var x=$.fn.combotree.getNode(node.nodes?node.nodes:[],id);
				if(x){
					return x;
				}
			}
			return null;
		}
		
		$.fn.combotree.methods={
			'options':function(jq){
				return jq.data('combotree');
			},
			'destroy':function(jq){
				var textInp=jq.next();
				textInp.remove();
				jq.show();
			},
			'getValue':function(jq){
				return jq.val();
			},
			'setValue':function(jq,value){
				var node=$.fn.combotree.methods.getNode(jq,value);
				if(node){
					jq.val(node.id);
					jq.next().find('input.form-control').val(node.text);
				}
			},
			'getNode':function(jq,id){
				var data=jq.data('combotree').data;
				return $.fn.combotree.getNode(data,id);
			},
			'clear':function(jq){
				jq.val('');
				jq.next().find('input.form-control').val('');
			}
			
		}
		
		$.fn.combotree.defaults={
			expandIcon: "glyphicon glyphicon-plus-sign",
			collapseIcon: "glyphicon glyphicon-minus-sign",
			emptyIcon:"glyphicon glyphicon-file",
			showBorder:false,
			showTags: true,
			url:'combotree',
			data:[]
		}
	
	/* 扩展combobox */
	function createUl(jq, data, options) {
		var ul = $('<ul class="dropdown-menu"/>');
		var val = options.val;
		for (var i = 0; i < data.length; i++) {
			var opt = data[i];
			var li = $('<li/>').val(opt.value).append($('<a/>').text(opt.text));
			if (val == opt.value) {
				li.addClass('active');
				jq.find('input.form-control').val(opt.text);
			}
			ul.append(li);
		}
		ul.find('li').click(function() {
			var self = $(this);
			var oldSelected = $(self.parent().find('.active'));
			var newVal = self.val();
			var oldVal = null;
			if (oldSelected) {
				oldVal = oldSelected.val();
				oldSelected.removeClass('active');
			}
			self.addClass('active');
			jq.prev().val(self.val());
			jq.find('input.form-control').val(self.find('a').text());
			var callback = options.onchange;
			options.val = self.val();
			if (oldVal !== newVal) {
				callback(newVal, oldVal);
			}
		})
		ul.appendTo(jq);
	}

	function createCombo(jq) {
		var data = jq.options.data;
		jq.hide();
		var div = $('<div class="dropdown input-group"/>');
		var hidebtn=$('<span class="input-group-btn"><button class="btn btn-default" type="button"><span class="glyphicon glyphicon-remove"></span></button></span>');
		var input = $('<input readonly class="dropdown-toggle form-control" data-toggle="dropdown"/>');
		input.appendTo(div);
		if(jq.options.hidebtn){
			div.append(hidebtn);
			hidebtn.find('button').click(function(){
				jq.combobox('setNull');
			});
			hidebtn.find('button').click(jq.options.hidebtn);
		}
		jq.after(div);
		
		if (data) {
			createUl(div, data, jq.options);
		} else {
			var valueField = jq.options.valueField;
			var textField = jq.options.textField;
			var url = jq.options.url;
			$.get(url, function(result) {
				data = [];
				for (var i = 0; i < result.length; i++) {
					var obj1 = result[i];
					var obj2 = {};
					obj2.value = obj1[valueField];
					obj2.text = obj1[textField];
					data.push(obj2);
				}
				jq.options.data = data;
				createUl(div, data, jq.options);
			})
		}
	}

	$.extend($.fn, {
		combobox: function(options, params) {
			if (typeof options == 'string') {
				var self = $(this);
				var method = $.fn.combobox.methods[options];
				return method(self, params);
			}
			options = options || {};

			$(this).each(function() {
				var self = $(this);
				var opts = self.data('combobox');
				if (opts) {
					options = $.extend({}, opts, options);
					$.fn.combobox.methods.destroy(self);
				}
				var optionsStr = '{' + this.dataset['options'] + '}';
				var val = self.val();
				var data_options = eval('(' + optionsStr + ')');
				self.options = $.extend({}, $.fn.combobox.defaults, data_options, options);
				self.options.val = val;
				self.data('combobox', self.options);
				createCombo(self);
			})
		}
	})

	$.fn.combobox.defaults = {
		textField: 'text',
		valueField: 'value',
		data: null,
		url: '',
		onchange: function(newVal, oldVal) {

		}
	};

	$.fn.combobox.setDefaults = function(options) {
		$.extend($.fn.combobox.defaults, options);
	};

	$.fn.combobox.methods = {
		'options': function(jq) {
			return jq.data('combobox');
		},
		'getValue': function(jq) {
			var options=jq.data('combobox');
			var val = options.val;
			return val;
		},
		'setValue': function(jq, val) {
			var options = jq.data('combobox');
			var data = options.data;
			var i;
			for (i = 0; i < data.length; i++) {
				if (data[i].value == val) {
					break;
				}
			}
			if (i < data.length) {
				self.options.val = val;
				jq.val(val);
				var div = jq.siblings('div.dropdown');
				div.find('input.form-control').val(data[i].text);
				div.find('ul>li').removeClass('active');
				$(div.find('ul>li')[i]).addClass('active');
			} else {
				$.fn.combobox.methods.setNull(jq);
			}
		},
		'setNull': function(jq) {
			var options = jq.data('combobox');
			options.val = '';
			jq.val('');
			var div = jq.siblings('div.dropdown');
			div.find('input.form-control').val('');
			div.find('ul>li').removeClass('active');
		},
		'destroy': function(jq) {
			jq.siblings('div.dropdown').remove();
			jq.removeData('combobox');
			jq.show();
		}
	};

	$.parser = {
		'plugins': ['combobox','datagrid','combotree'],
		'parse': function(context) {
			for (var i = 0; i < $.parser.plugins.length; i++) {
				(function() {
					// 控件名
					var name = $.parser.plugins[i];
					// 查找class为easyui-控件名的jq对象，例如，easyui-layout
					var r = $('.' + name, context);

					if (r.length) {
						// 如果有这个对象，那么判断它有没有初始化函数
						if (r[name]) {
							// 如果有直接调用
							r[name]();
						}
					}
				})();
			}
		}
	}
	
	$(function() {
		$.parser.parse();
	})
})($);